Replace Function

Replaces the first occurrence of a String with another String.

Syntax

result = Replace( sourceString, oldString, newString )

result = stringVariable.Replace( oldString, newString )


Parameters

sourceString

String

The original string.

oldString

String

The characters to be replaced.

newString

String

The replacement characters.



Notes

Replaces the first occurrence of oldString in sourceString with newString. Replace is case-insensitive.

If newString is an empty string (""), the Replace function deletes the first occurrence of the oldString in the sourceString.

If oldString is an empty string (""), the Replace function returns an unchanged copy of the sourceString.


Examples

Below are some examples that show the results of the Replace function:

Dim result As String
result=Replace ("The quick fox","fox","rabbit") //returns "The quick rabbit"
result=Replace("The quick fox","f","b") //returns "The quick box"
result=Replace("The quick fox","quick","") //returns "The fox"

Using the second syntax:

Dim result,s As String
s="The quick fox"
result=s.Replace ("fox","rabbit") //returns "The quick rabbit"
MsgBox result

See Also

ReplaceAll, ReplaceAllB, ReplaceB, ReplaceLineEndings functions.